home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / Misc. C ƒ / Recorder / Old / Synchronous Recorder.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-03  |  4.4 KB  |  188 lines  |  [TEXT/MPS ]

  1. /*    File: Recorder.c
  2.  
  3.     MPW Tool for recording serial line traffic
  4.     
  5. */
  6.  
  7.  
  8. #include <Types.h>
  9. #include <QuickDraw.h>
  10. #include <CursorCtl.h>
  11.  
  12. #include <CRMIntf.h>
  13. #include <CTBUtils.h>
  14. #include <CMIntf.h>
  15. #include <FTIntf.h>
  16. #include <TMIntf.h>
  17.  
  18. #include <StdIO.h>
  19. #include <StdLib.h>
  20.  
  21.  
  22. #define    CommToolBoxTrap        0x8B
  23. #define    UnimplementedTrap    0x9F
  24. #define    Check(err,str)    { \
  25.                         OSErr errXYZZY; \
  26.                         if (( errXYZZY = ( err )) != noErr ) \
  27.                             fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
  28.                         }
  29. #define    INCONFIGSTR        "Baud 9600 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
  30.                         "Handshake None HoldConnection False RemindDisconnect False"
  31. #define    OUTCONFIGSTR    "Baud 9600 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
  32.                         "Handshake None HoldConnection False RemindDisconnect False"
  33.  
  34. #define        BUF_SIZE    1024
  35.  
  36. short        inProcID;
  37. short        outProcID;
  38. ConnHandle    inputStream;
  39. ConnHandle    outputStream;
  40. char        inBuffer  [ BUF_SIZE ];
  41. char        outBuffer [ BUF_SIZE ];
  42. CMFlags        inFlags;
  43. CMStatFlags    inStatFlags;
  44.  
  45. /*    Is the Comm Toolbox actually installed ?? */
  46. Boolean        IsCTBInstalled    ( ) {
  47.     return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
  48.             NGetTrapAddress ( CommToolBoxTrap, OSTrap );
  49.     }
  50.  
  51. short    InitAll ( void ) {
  52.     OSErr    err;
  53.  
  54.     InitGraf ( &qd.thePort );
  55. /*
  56.     InitFonts ();
  57.     InitWindows ();
  58.     InitMenus ();
  59.     TEInit ();
  60.     InitDialogs ( NULL );
  61.     InitCursor ();
  62. */
  63.  
  64.     InitCursorCtl ( NULL );
  65.     
  66.     if ( !IsCTBInstalled ) {
  67.         fprintf ( stderr, "Comm Toolbox not installed!\n" );
  68.         return 1;
  69.         }
  70.  
  71. /*    Load up the Communications Toolbox */
  72.     (void) InitCTBUtilities ();
  73.     (void) InitCRM ();
  74.  
  75.     err = InitTM ();
  76.     if ( err == tmNoTools ) {
  77.         fprintf ( stderr, "No terminal tools found\n" );
  78.         return 2;
  79.         }
  80.     
  81.     err = InitCM ();
  82.     if ( err == cmNoTools ) {
  83.         fprintf ( stderr, "No connection tools found\n" );
  84.         return 2;
  85.         }
  86.     
  87.     err = InitFT ();
  88.     if ( err == ftNoTools ) {
  89.         fprintf ( stderr, "No file transfer tools found\n" );
  90.         return 2;
  91.         }        
  92.  
  93.     return 0;
  94.     }
  95.  
  96.  
  97. void ExitProc ( void ) {
  98.     
  99. /*    Close the connection and dispose of the connection record */
  100.     if ( inputStream != NULL ) {
  101.         Check ( CMClose ( inputStream, false, NULL, -1, false ), "CMCLose - input" );
  102.         CMDispose ( inputStream );
  103.         }
  104.  
  105.     if ( outputStream != NULL ) {
  106.         Check ( CMClose ( outputStream, false, NULL, -1, false ), "CMCLose - output" );
  107.         CMDispose ( outputStream );
  108.         }
  109.     }
  110.  
  111. ConnHandle    InitStream ( Boolean isIn, short *procID ) {
  112.     CMBufferSizes    bSize;
  113.  
  114. /*    Open a connection tool */
  115.     bSize [ cmDataIn ] = 0;        bSize [ cmDataOut ] = 0;
  116.     bSize [ cmCntlIn ] = 0;        bSize [ cmCntlOut ] = 0;
  117.     bSize [ cmAttnIn ] = 0;        bSize [ cmAttnOut ] = 0;
  118.     bSize [ cmRsrvIn ] = 0;        bSize [ cmRsrvOut ] = 0;
  119.     if ( isIn )
  120.         bSize [ cmDataIn  ] = BUF_SIZE;
  121.     else
  122.         bSize [ cmDataOut ] = BUF_SIZE;
  123.     *procID = CMGetProcID ( "\pSerial" );
  124.     return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
  125.     }
  126.                             
  127.  
  128.  
  129. int main ( int argc, char *argv[] ) {
  130.     short    err;
  131.     long    cnt;
  132.     
  133.     if ( err = InitAll ( ) != 0 )
  134.         exit ( err );
  135.         
  136.     atexit ( ExitProc );
  137.  
  138. /*    Open a connection tool */
  139.     inputStream = InitStream ( true, &inProcID );
  140.     if ( inputStream == NULL ) {
  141.         fprintf ( stderr, "Cannot create input handle\n" );
  142.         return 3;
  143.         }
  144. /*    Open a connection tool */
  145.     outputStream = InitStream ( false, &outProcID );
  146.     if ( inputStream == NULL ) {
  147.         fprintf ( stderr, "Cannot create out handle\n" );
  148.         return 3;
  149.         }
  150.                             
  151.                             
  152.     
  153. /*    Configure the connection */
  154.     Check ( CMSetConfig ( inputStream, INCONFIGSTR ), "CMSetConfig - In" );
  155.     Check ( CMOpen ( inputStream, false, NULL, -1 ), "CMOpen - In" );
  156.     Check ( CMListen ( inputStream, false, NULL, -1 ), "CMListen - In" );
  157.  
  158.     Check ( CMSetConfig ( outputStream, OUTCONFIGSTR ), "CMSetConfig - Out" );
  159.     Check ( CMOpen ( outputStream, false, NULL, -1 ), "CMOpen - Out" );
  160.     Check ( CMListen ( outputStream, false, NULL, -1 ), "CMListen - Out" );
  161.     
  162.     cnt = 0;
  163.     while ( true ) {
  164.         CMBufferSizes    bSize;
  165.         
  166.         cnt++;
  167.         if ( cnt % 32 == 0 ) {
  168.             CMIdle ( inputStream );
  169.             CMIdle ( outputStream );
  170.             SpinCursor ( 1 );
  171.             }
  172.             
  173.     /*    read data and print it out */
  174.         bSize [ cmDataIn ] = 0;
  175.         Check ( CMStatus ( inputStream, bSize, &inStatFlags ), "CMStatus" );
  176.         if ( bSize [ cmDataIn ] > 0 ) {
  177.             Check ( CMRead ( inputStream, inBuffer, &bSize [ cmDataIn ], 
  178.                                 cmData, false, NULL, -1, &inFlags ), "CMRead" );
  179.             Check ( CMWrite ( outputStream, inBuffer, &bSize [ cmDataIn ], 
  180.                                 cmData, false, NULL, -1,  inFlags ), "CMWrite" );
  181.             }
  182.         
  183.             
  184.         }
  185.  
  186.     return 0;
  187.     }
  188.